package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0209.java
 * @Author : WangRuoyu
 * @Date 2023/11/6 20:43
 */


public class Q0209 {
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;
        int i = 0;
        int sum = 0;
        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            if (sum >= target) {
                while (sum >= target) {
                    sum -= nums[i++];
                }
                res = Math.min(res, j - i + 2);
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }

    public static void main(String[] args) {
        Q0209 sl = new Q0209();
        int target = 7;
        int[] nums = {2, 3, 1, 2, 4, 3};
        System.out.println(sl.minSubArrayLen(target, nums));
    }
}